<?php

namespace app\admin\controller\polite;

use app\admin\model\PoliteArticle;
use app\admin\traits\Curd;
use app\common\controller\AdminController;
use think\App;
use EasyAdmin\annotation\NodeAnotation;
use EasyAdmin\annotation\ControllerAnnotation;
use function GuzzleHttp\default_user_agent;

/**
 * Class Goods
 * @package app\admin\controller\polite
 * @ControllerAnnotation(title="文章管理")
 */
class Article extends AdminController
{

    use Curd;

    protected $relationSearch = false;
    protected $searchType;
    public function __construct(App $app)
    {
        parent::__construct($app);
        $searchType= (new \app\admin\service\PoliteConfigService)->getConfigByTypeName('Pc文章类型');
        $newSearchType=[];
        foreach ($searchType as $k=>$v){
            $newSearchType[$v['id']]=$v['config_value'];
        }
        $this->searchType=json_encode($newSearchType,true);
        $this->model = new PoliteArticle();
    }

    /**
     * @NodeAnotation(title="文章列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $admin=session('admin');
            if($admin['type']==3){
                //乡镇管理员，显示自己乡镇的数据
                $where[]=['township','=',$admin['area_id']];
            }
            $count = $this->model->where($where)->count();
            $list = $this->model->where($where)->page($page, $limit)->order($this->sort)->select();
            foreach ($list as $k=>$v){
                $list[$k]=$this->resetInfo($v);
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }else{
            $this->assign('searchType',$this->searchType);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="添加文章")
     */
    public function add($id = null)
    {
        $articleType= (new \app\admin\service\PoliteConfigService())->getConfigByTypeName('Pc文章类型');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [
                'title|标题'   => 'require',
                'type|文章类型'  => 'require',
                'cover_photo|封面图片'  => 'require',
                'content|公示内容'  => 'require',
            ];
            $this->validate($post, $rule);
            $post['township']=$this->admin['area_id'];
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            if ($save) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }else{
            $this->assign('searchType',$this->searchType);
        }
        $this->assign([
            'articleType'=>$articleType,
        ]);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑文章")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        $row=$this->resetInfo($row);
        $articleType= (new \app\admin\service\PoliteConfigService)->getConfigByTypeName('Pc文章类型');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [
                'title|标题'   => 'require',
                'type|文章类型'  => 'require',
                'cover_photo|封面图片'  => 'require',
                'content|公示内容'  => 'require',
            ];
            $this->validate($post, $rule);
            $post['township']=$this->admin['area_id'];
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            if ($save) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }else{
            $this->assign('searchType',$this->searchType);
        }
        $this->assign([
            'id'          => $id,
            'row'         => $row,
            'articleType'=>$articleType,
        ]);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="删除文章")
     */
    public function delete($id)
    {
        $row = $this->model->whereIn('id', $id)->select();
        $row->isEmpty() && $this->error('数据不存在');
        try {
            $save = $row->delete();
        } catch (\Exception $e) {
            $this->error('删除失败');
        }
        $save ? $this->success('删除成功') : $this->error('删除失败');
    }

    private function resetInfo($data){
        if($data){
            $data['type_text']=!empty($data['type'])? (new \app\admin\service\PoliteConfigService)->getConfigById($data['type'])['config_value']:'';
        }
        return $data;
    }
}